<?php
/**
 * Created by PhpStorm.
 * User: aa
 * Date: 15/10/21
 * Time: 下午3:54
 */
include_once "FoodServer.php";
include_once "Image.php";
$foodServel = new FoodServer();
$image = new Image();
//获取所有的分类
//$result = $foodServel->getAllCategory();
//foreach ($result as $item) {
$url = 'http://food.boohee.com/fb/v1/categories?kind=cooking';
$categoryResult = _getResult($url);
foreach ($categoryResult->categories as $itemResult) {
	$info = $foodServel->getInfoByTitle($itemResult->name);
	if (empty($info)) {
		continue;
	}
	for ($n = 0; $n <= 1; $n++) {//排序0--降序1--升序
		for ($m = 1; $m <= 4; $m++) {//1--常见2--热量3--蛋白质 4--脂肪
			for ($i = 1; $i <= 10; $i++) {//每页十条
				$url = 'http://food.boohee.com/fb/v1/foods?order_asc=' . $n . '&page=' . $i . '&value=' . $itemResult->id . '&kind=cooking&order_by=' . $m;
				//echo $url . "<br>";
				$jsonData = _getResult($url);
				_getDetail($info['id'], $foodServel, $image, $jsonData->foods, $itemResult->name);

				if (!empty($itemResult->sub_categories)) {//判断有子类
					foreach ($itemResult->sub_categories as $subItem) {
						$subInfo = $foodServel->getInfoByTitle($subItem->name);
						if (empty($subInfo)) {
							continue;
						}
						$path = 'http://food.boohee.com/fb/v1/foods?order_asc=' . $n . '&page=' . $i . '&sub_value=' . $subItem->id . '&value=' . $itemResult->id . '&kind=cooking&order_by=' . $m;
						//echo $path."<br>";
						$jsonSubData = _getResult($path);
						_getDetail($info['id'], $foodServel, $image, $jsonSubData->foods, $itemResult->name, $subItem->name);
					}
				}
			}

		}
	}
}

//}

/**
 * 通过url获取json数据
 * @param $url
 * @return mixed
 */
function _getResult($url) {
	$result = file_get_contents($url);
	$jsonResult = json_decode($result);
	return $jsonResult;
}

/**
 * 获取食物详情
 * @param $data
 * @param $categoryName
 * @param $subCategoryName
 */
function _getDetail($categoryIds, $foodServel, $image, $data, $categoryName, $subCategoryName = '') {
	if (!empty($data)) {
		foreach ($data as $item) {
			$url = 'http://food.boohee.com/fb/v1/foods/' . $item->code;
			$result = _getResult($url);
			//var_dump($result->units);die();
			//判断食物是否存在
			$foodInfos = $foodServel->getFoodsInfoByName($result->name);
			//var_dump($foodInfos['id']);die();
			if ($foodInfos) {
				$foodId = $foodInfos['id'];
				//查询食物分类
				$categoryId = 0;
				$subCategoryId = 0;
				$info = $foodServel->getInfoByTitle($categoryName);
				if (!empty($info)) {
					$categoryId = $info['id'];
				}
				if (!empty($subCategoryName)) {
					$subInfo = $foodServel->getInfoByTitle($subCategoryName);
					if (!empty($subInfo)) {
						$subCategoryId = $subInfo['id'];
					}
				}
				if (empty($categoryId)) {
					continue;
				}
				//添加分类关系
				_addRelation($foodServel, $foodId, $categoryId, $subCategoryId);
				//echo "添加成功";
			} else {
				$foodId = _addFoodsInfo($categoryIds, $foodServel, $result, $image);
				if (empty($foodId)) {
					continue;
				}
				//添加食物成分
				_addUnitsInfo($foodServel, $foodId, $result->units);
				//查询食物分类
				$categoryId = 0;
				$subCategoryId = 0;
				$info = $foodServel->getInfoByTitle($categoryName);
				if (!empty($info)) {
					$categoryId = $info['id'];
				}
				if (!empty($subCategoryName)) {
					$subInfo = $foodServel->getInfoByTitle($subCategoryName);
					if (!empty($subInfo)) {
						$subCategoryId = $subInfo['id'];
					}
				}
				if (empty($categoryId)) {
					continue;
				}
				//添加分类关系
				_addRelation($foodServel, $foodId, $categoryId, $subCategoryId);
				echo "添加成功";

			}

		}
	}
}

/**
 * 添加食物
 * @param $info
 * @return int
 */
function _addFoodsInfo($categoryIds, $foodServel, $info, $image) {
	if (!empty($info)) {
		$data['code'] = "'" . $info->code . "'"; // . $info->code . '";
		$data['name'] = "'" . $info->name . "'";//"'".$info->name."'";
		$imagePath = 'uploads/food/info/';
		$thumbImageUrl = '';
		$largeImageUrl = '';
		if ($info->thumb_image_url != 'http://s.boohee.cn/house/food_mid/m_mianbao .jpg') {
			if (!empty($info->thumb_image_url)) {
				$thumbImageUrl = _saveImage($image, $info->thumb_image_url, $imagePath, $categoryIds);
			}

			if (!empty($info->large_image_url)) {
				$largeImageUrl = _saveImage($image, $info->large_image_url, $imagePath, $categoryIds);
			}
		}
		$data['thumb_image_url'] = "'" . $thumbImageUrl . "'";// $thumbImageUrl;//下载图片
		$data['large_image_url'] = "'" . $largeImageUrl . "'";//$largeImageUrl;//下载图片
		$appraise = $info->appraise;
		if (empty($info->appraise)) {
			$appraise = 0;
		}
		$data['appraise'] = "'" . $appraise . "'";//$info->appraise;
		$weight = $info->weight;
		if (empty($info->weight)) {
			$weight = 0;
		}
		$data['weight'] = "'" . $weight . "'";

		$health_light = $info->health_light;
		if (empty($info->health_light)) {
			$health_light = 0;
		}
		$data['health_light'] = "'" . $health_light . "'";

		if (empty($info->is_liquid)) {
			$is_liquid = 0;
		} else {
			$is_liquid = 1;
		}
		$data['is_liquid'] = "'" . $is_liquid . "'";


		$calory = $info->ingredient->calory;
		if (empty($info->ingredient->calory)) {
			$calory = 0;
		}
		$data['calory'] = "'" . $calory . "'";
		$protein = $info->ingredient->protein;
		if (empty($info->ingredient->protein)) {
			$protein = 0;
		}
		$data['protein'] = "'" . $protein . "'";

		$fat = $info->ingredient->fat;
		if (empty($info->ingredient->fat)) {
			$fat = 0;
		}
		$data['fat'] = "'" . $fat . "'";
		$carbohydrate = $info->ingredient->carbohydrate;
		if (empty($info->ingredient->carbohydrate)) {
			$carbohydrate = 0;
		}
		$data['carbohydrate'] = "'" . $carbohydrate . "'";

		$fiber_dietary = $info->ingredient->fiber_dietary;
		if (empty($info->ingredient->fiber_dietary)) {
			$fiber_dietary = 0;
		}

		$data['fiber_dietary'] = "'" . $fiber_dietary . "'";

		$vitamin_a = $info->ingredient->vitamin_a;
		if (empty($info->ingredient->vitamin_a)) {
			$vitamin_a = 0;
		}

		$data['vitamin_a'] = "'" . $vitamin_a . "'";

		$vitamin_c = $info->ingredient->vitamin_c;
		if (empty($info->ingredient->vitamin_c)) {
			$vitamin_c = 0;
		}
		$data['vitamin_c'] = "'" . $vitamin_c . "'";
		$vitamin_e = $info->ingredient->vitamin_e;
		if (empty($info->ingredient->vitamin_e)) {
			$vitamin_e = 0;
		}
		$data['vitamin_e'] = "'" . $vitamin_e . "'";

		$carotene = $info->ingredient->carotene;
		if (empty($info->ingredient->carotene)) {
			$carotene = 0;
		}
		$data['carotene'] = "'" . $carotene . "'";

		$thiamine = $info->ingredient->thiamine;
		if (empty($info->ingredient->thiamine)) {
			$thiamine = 0;
		}
		$data['thiamine'] = "'" . $thiamine . "'";
		$lactoflavin = $info->ingredient->lactoflavin;
		if (empty($info->ingredient->lactoflavin)) {
			$lactoflavin = 0;
		}
		$data['lactoflavin'] = "'" . $lactoflavin . "'";

		$niacin = $info->ingredient->niacin;
		if (empty($info->ingredient->niacin)) {
			$niacin = 0;
		}
		$data['niacin'] = "'" . $niacin . "'";
		$cholesterol = $info->ingredient->cholesterol;
		if (empty($info->ingredient->cholesterol)) {
			$cholesterol = 0;
		}
		$data['cholesterol'] = "'" . $cholesterol . "'";
		$magnesium = $info->ingredient->magnesium;
		if (empty($info->ingredient->magnesium)) {
			$magnesium = 0;
		}
		$data['magnesium'] = "'" . $magnesium . "'";
		$calcium = $info->ingredient->calcium;
		if (empty($info->ingredient->calcium)) {
			$calcium = 0;
		}

		$data['calcium'] = "'" . $calcium . "'";
		$iron = $info->ingredient->iron;
		if (empty($info->ingredient->iron)) {
			$iron = 0;
		}
		$data['iron'] = "'" . $iron . "'";

		$zinc = $info->ingredient->zinc;
		if (empty($info->ingredient->zinc)) {
			$zinc = 0;
		}
		$data['zinc'] = "'" . $zinc . "'";
		$copper = $info->ingredient->copper;
		if (empty($info->ingredient->copper)) {
			$copper = 0;
		}

		$data['copper'] = "'" . $copper . "'";
		$manganese = $info->ingredient->manganese;
		if (empty($info->ingredient->manganese)) {
			$manganese = 0;
		}
		$data['manganese'] = "'" . $manganese . "'";
		$kalium = $info->ingredient->kalium;
		if (empty($info->ingredient->kalium)) {
			$kalium = 0;
		}
		$data['kalium'] = "'" . $kalium . "'";

		$phosphor = $info->ingredient->phosphor;
		if (empty($info->ingredient->phosphor)) {
			$phosphor = 0;
		}

		$data['phosphor'] = "'" . $phosphor . "'";

		$natrium = $info->ingredient->natrium;
		if (empty($natrium)) {
			$natrium = 0;
		}
		$data['natrium'] = "'" . $natrium . "'";
		$selenium = $info->ingredient->selenium;
		if (empty($info->ingredient->selenium)) {
			$selenium = 0;
		}
		$data['selenium'] = "'" . $selenium . "'";

		$valueArray = array_values($data);
		$valueString = implode(",", $valueArray);
		$keyArray = array_keys($data);
		$keyString = implode(",", $keyArray);
		$id = $foodServel->addFoods($keyString, $valueString);
		return $id;
	}
}

/**
 * 图片保存本地
 * @param $image
 * @param $url
 * @param string $path
 * @param $categoryIds
 * @return string
 */
function _saveImage($image, $url, $path = 'uploads/food/category/', $categoryIds = 0) {
	$extension = pathinfo($url, PATHINFO_EXTENSION);
	if (empty($categoryIds)) {
		$name = date('Y/md/') . uniqid() . '.' . $extension;
	} else {
		$name = date('Y/md/') . $categoryIds . '/' . uniqid() . '.' . $extension;
	}

	$imgPath = $path . $name;
	$image->load($url)->save($imgPath);
	return $name;
}

/**
 * 添加食物成分表
 * @param $foodId
 * @param $result
 */
function _addUnitsInfo($foodServel, $foodId, $result) {
	if (!empty($result) && !empty($foodId)) {
		foreach ($result as $item) {
			$data['food_id'] = $foodId;
			$data['amount'] = "'" . $item->amount . "'";
			$data['unit'] = "'" . $item->unit . "'";
			$data['weight'] = "'" . $item->weight . "'";
			$data['calory'] = "'" . $item->calory . "'";
			$valueArray = array_values($data);
			$valueString = implode(",", $valueArray);
			$keyArray = array_keys($data);
			$keyString = implode(",", $keyArray);
			$id = $foodServel->addFoodsUnitsInfo($keyString, $valueString);
		}
	}
}

/**
 * 添加食物分类关系表
 * @param $foodId
 * @param int $categoryId
 * @param int $subCategoryId
 */
function _addRelation($foodServel, $foodId, $categoryId = 0, $subCategoryId = 0) {
	//判断分类和食物是否存在
	$relationInfo = $foodServel->isExistRelation($foodId, $categoryId);
	if ($relationInfo) {
		if (!empty($subCategoryId)) {
			$relationsubInfo = $foodServel->isExistRelation($foodId, $categoryId, $subCategoryId);
			if (empty($relationsubInfo)) {
				//修改分类
				$foodServel->modifyRelation($foodId, $categoryId, $subCategoryId);
			}
		}
	} else {
		//添加分类和食物的关系
		$valueString = "'" . $foodId . "','" . $categoryId . "','" . $subCategoryId . "'";
		$keyString = "food_id,category_id,catagory_sub_id";
		$foodServel->addRelation($keyString, $valueString);
	}
}